package com.monitoring.application.service;

import com.monitoring.application.dto.DeviceDTO;
import com.monitoring.domain.entity.Device;
import com.monitoring.domain.entity.AirConditioner;
import com.monitoring.domain.entity.ElectricMeter;
import com.monitoring.domain.service.DeviceService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class DeviceApplicationService {
    private final DeviceService deviceService;
    
    public DeviceApplicationService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }
    
    // 创建设备（根据类型）
    public DeviceDTO createDevice(DeviceDTO deviceDTO) {
        Device device;
        if ("AIR_CONDITIONER".equals(deviceDTO.getType())) {
            device = deviceService.createAirConditioner(
                    deviceDTO.getName(),
                    deviceDTO.getLocation()
            );
        } else if ("ELECTRIC_METER".equals(deviceDTO.getType())) {
            device = deviceService.createElectricMeter(
                    deviceDTO.getName(),
                    deviceDTO.getLocation()
            );
        } else {
            throw new IllegalArgumentException("不支持的设备类型");
        }
        return convertToDTO(device);
    }
    
    // 根据ID获取设备
    public Optional<DeviceDTO> getDeviceById(Long id) {
        return deviceService.getDeviceById(id)
                .map(this::convertToDTO);
    }
    
    // 获取所有设备
    public List<DeviceDTO> getAllDevices() {
        return deviceService.getAllDevices().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    // 根据类型获取设备
    public List<DeviceDTO> getDevicesByType(String type) {
        return deviceService.getDevicesByType(type).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    // 根据位置获取设备
    public List<DeviceDTO> getDevicesByLocation(String location) {
        return deviceService.getDevicesByLocation(location).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    // 打开设备
    public DeviceDTO turnOnDevice(Long id) {
        Device device = deviceService.turnOnDevice(id);
        return convertToDTO(device);
    }
    
    // 关闭设备
    public DeviceDTO turnOffDevice(Long id) {
        Device device = deviceService.turnOffDevice(id);
        return convertToDTO(device);
    }
    
    // 激活设备
    public DeviceDTO activateDevice(Long id) {
        Device device = deviceService.activateDevice(id);
        return convertToDTO(device);
    }
    
    // 停用设备
    public DeviceDTO deactivateDevice(Long id) {
        Device device = deviceService.deactivateDevice(id);
        return convertToDTO(device);
    }
    
    // 设备维护
    public DeviceDTO putDeviceInMaintenance(Long id) {
        Device device = deviceService.putDeviceInMaintenance(id);
        return convertToDTO(device);
    }
    
    // 恢复设备运行
    public DeviceDTO resumeDeviceOperation(Long id) {
        Device device = deviceService.resumeDeviceOperation(id);
        return convertToDTO(device);
    }
    
    // 删除设备
    public void deleteDevice(Long id) {
        deviceService.deleteDevice(id);
    }
    
    // 获取活跃设备
    public List<DeviceDTO> getActiveDevices() {
        return deviceService.getActiveDevices().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    // 转换实体到DTO
    private DeviceDTO convertToDTO(Device device) {
        DeviceDTO dto = new DeviceDTO();
        dto.setId(device.getId());
        dto.setName(device.getName());
        dto.setType(device.getType());
        dto.setLocation(device.getLocation());
        dto.setActive(device.isActive());
        dto.setPoweredOn(device.isPoweredOn());
        dto.setStatus(device.getStatus());
        dto.setCreatedAt(device.getCreatedAt());
        dto.setUpdatedAt(device.getUpdatedAt());
        
        // 处理空调特有属性
        if (device instanceof AirConditioner) {
            AirConditioner ac = (AirConditioner) device;
            dto.setTemperature(ac.getTemperature());
            dto.setMode(ac.getMode());
            dto.setFanSpeed(ac.getFanSpeed());
            dto.setEnergySavingMode(ac.isEnergySavingMode());
        }
        
        // 处理电表特有属性
        if (device instanceof ElectricMeter) {
            ElectricMeter meter = (ElectricMeter) device;
            dto.setCurrentReading(meter.getCurrentReading());
            dto.setTotalConsumption(meter.getTotalConsumption());
            dto.setPowerFactor(meter.getPowerFactor());
            dto.setVoltage(meter.getVoltage());
            dto.setCurrent(meter.getCurrent());
        }
        
        return dto;
    }
}